﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Forget.HRBLL.View;
using System.Data;
using Forget.Data;
using Forget.HRBLL.Model;

namespace Forget.HRBLL.Logical
{
    public class DepartmentLogical
    {
        public static List<Department> GetAllDepartment()
        {
            return Department.GetAllDepartment();
        }

        public static Department GetDepart_ID(int? departID)
        {
            if (departID!=null && departID > 0)
                return Department.GetDepart_ID(new Department() { DepartmentID = departID });
            return null;
        }

        public static bool AddDepartment(string name, int parentID)
        {
            if (!string.IsNullOrEmpty(name) && parentID >= 0)
                return Department.AddDepartment(new Department() { Name = name, ParentID = parentID });

            return false;
        }

        public static bool DelDepartment(int departmentID)
        {
            if (departmentID > 0)
                return Department.DelDepartment(new Department() { DepartmentID = departmentID });
            return false;
        }

        public static bool EditDepartment(int departmentID, string name)
        {
            if (!string.IsNullOrEmpty(name) && departmentID > 0)
                return Department.EditDepartment(new Department() { DepartmentID = departmentID, Name = name });
            return false;
        }
    }
}

namespace Forget.HRBLL.Model
{
    public partial class Department
    {
        private static readonly string SELECTALLDEPARTMET = "HR_SelectDepartment";
        private static readonly string SELECT_DEPARTID = "HR_SelectDepart_ID";

        public static List<Department> GetAllDepartment()
        {
            List<Department> departs = null;
            try
            {
                DataTable table = DataAccess.ExecuteDataTable(CommandType.StoredProcedure, SELECTALLDEPARTMET, null);
                departs = Convert<Department>.DataConvertToObject(table);
            }
            catch (Exception ex)
            {
                Log.WriteLog("BU.DepartGetAll", ex.Message, ex.StackTrace);
            }
            return departs;
        }

        public static Department GetDepart_ID(Department dp)
        {
            List<SqlParameter> parms = Convert<SqlParameter>.ObjectToParms(dp);
            List<Department> dps = null;
            try
            {
                DataTable table = DataAccess.ExecuteDataTable(CommandType.StoredProcedure, SELECT_DEPARTID, parms.ToArray());
                dps = Convert<Department>.DataConvertToObject(table);
            }
            catch (Exception ex)
            {
                Log.WriteLog("BU.SelEmpInfo_NumID", ex.Message, ex.StackTrace);
            }
            if (dps != null && dps.Count > 0)
                return dps[0];
            return null;
        }
    }
}